Miller-Rabin randomized primality test (1976, 1980)

There is a randomized algorithm running in O(n3log2(1/δ))O(n^3 \log_2(1/\delta)) time that, with probability 1δ1-\delta determines if an nn-bit integer xx is prime.

MillerRabin(n) If n>2n > 2 and nn is even, return composite. /* Factor n1n − 1 as 2st2^s t where tt is odd. */ s0s ← 0 tn1t ← n − 1 while tt is even   ss+1s ← s + 1   tt/2t ← t/2 end /* Done. n1=2stn − 1 = 2^s t. */ Choose x{1,2,...,n1}x ∈ \{1, 2, . . . , n − 1\} uniformly at random. Compute each of the numbers xt,x2t,x4t,...,x2st=xn1modnx_t , x^{2t} , x^{4t} , . . . , x^{2^st} = x^{n−1} \mod n. If xn11(modn)x^{n−1} \not\equiv 1 \pmod n, return composite. for i=1,2,...,si = 1, 2, . . . , s   If x2it1(modn)x^{2^i t} ≡ 1 \pmod n and x2i1t±1(modn)x^{2^{i−1} t} \not\equiv ±1 \pmod n, return composite. end /* Done checking for fake square roots. */ Return probably prime.


See also: PRIMES is in P paper

References:

  1. G. L. Miller, “Riemann’s hypothesis and tests for primality,” Proceedings of seventh annual ACM symposium on Theory of computing  - STOC ’75, pp. 234–239, May 1975. doi: 10.1145/800116.803773
  2. M. O. Rabin, “Probabilistic algorithm for testing primality,” Journal of Number Theory, vol. 12, no. 1, pp. 128–138, 1980. doi: 10.1016/0022-314x(80)90084-0
  3. https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
  4. https://www.cs.cornell.edu/courses/cs4820/2010sp/handouts/MillerRabin.pdf